package ysq.xyg.host.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.host.dao.HostCityDao;
import ysq.xyg.host.model.po.HostCity;
import ysq.xyg.sys.model.po.SysDictContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.host.model.vo.HostCityVo;
import ysq.xyg.host.service.HostCityService;
import ysq.xyg.web.model.po.WebColumnType;
import ysq.xyg.web.model.vo.WebColumnTypeVo;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 房产模块-城市service实现
 *
 * @author shengQiang yu
 * @date 2019-7-1 13:49:33
 */
@Service("hostCityService")
public class HostCityServiceImpl extends BaseServiceImpl<HostCity> implements HostCityService {

    @Resource
    private HostCityDao hostCityDao;

    private HostCity hostCity;

    @Autowired
    public void setBaseDao(HostCityDao hostCityDao) {
        this.baseDao = hostCityDao;
    }


    /**
     * 分页查询
     *
     * @param hostCityVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 13:49:33
     */
    public ReturnEntity page(HostCityVo hostCityVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" sort desc,update_time desc");
        List<HostCity> list = acceptList(hostCityVo);
        PageInfo<HostCity> page=new PageInfo<HostCity>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostCityVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 13:49:33
     */
    public ReturnEntity addOrUpdate(HostCityVo hostCityVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(hostCityVo.getId())){
            hostCity=new HostCity(currentUser);
            isUpate=false;
        }else{
            hostCity=selectByPrimaryKey(hostCityVo.getId());
            if(ObjectUtil.isNull(hostCity)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            hostCity.setUpdateTime(new Date());
            hostCity.setUpdateUser(currentUser.getId());
        }
        hostCity.setSaleNum(hostCityVo.getSaleNum());
        hostCity.setPrice(hostCityVo.getPrice());
        hostCity.setName(hostCityVo.getName());
        hostCity.setName(hostCityVo.getName());
        hostCity.setSort(hostCityVo.getSort());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(hostCity);
        }else{
            num=insert(hostCity);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-7-1 13:49:33
     */
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostCity hostCity=selectByPrimaryKey(id);
        if(hostCity!=null){
            hostCity.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostCity.setUpdateTime(new Date());
            hostCity.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(hostCity);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }
    /**
     * 获取下拉框集合
     *
     * @param code code值
     * @author shengQiang yu
     * @date 2019-4-25 16:49:54
     */
    public ReturnEntity acceptSelect(String code) {
        List<HostCity> list = acceptList(new HostCityVo());
        List<SysDictContent> sysDictContentList=new ArrayList<SysDictContent>();
        for(HostCity hostCity:list){
            SysDictContent sysDictContent=new SysDictContent();
            sysDictContent.setId(hostCity.getId());
            sysDictContent.setName(hostCity.getName());
            sysDictContent.setDictType(code);
            sysDictContent.setValue(hostCity.getId());
            sysDictContentList.add(sysDictContent);
        }
        return new ReturnEntity(sysDictContentList);
    }
    /**
     * 获取集合
     *
     * @param hostCityVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-7-1 13:49:33
     */
    public List<HostCity> acceptList(HostCityVo hostCityVo){
        Example example = new Example(HostCity.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(hostCityVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",hostCityVo.getRemoveTag());
        }
        if(StringUtil.isNotEmpty(hostCityVo.getName())){
            criteria.andLike("name","%"+hostCityVo.getName()+"%");
        }
        example.setOrderByClause(" sort desc,update_time desc");
        return hostCityDao.selectByExample(example);
    }
}
